<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <el-row>
        <el-col :span="6">
          <el-input placeholder="请输入内容">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 订单列表 -->
      <el-table :data="ordersLists" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="订单编号" prop="order_number"></el-table-column>
        <el-table-column label="订单价格" prop="order_price" width="80px"></el-table-column>
        <el-table-column label="是否付款" width="80px">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.pay_status === '1'">已付款</el-tag>
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="is_send" width="80px"></el-table-column>
        <el-table-column label="下单时间" prop="create_time">
          <template slot-scope="scope">{{scope.row.create_time | dateFormat}}</template>
        </el-table-column>
        <el-table-column label="操作" width="125px">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="handelEditOrder(scope.row)"
            ></el-button>
            <el-button size="mini" type="success" icon="el-icon-location" @click="checkLogistics"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!--  页码区域 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryData.pagenum"
      :page-sizes="[3, 5, 10, 50]"
      :page-size="queryData.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
    <!-- 编辑地址对话框 -->
    <el-dialog title="填写地址" :visible.sync="addressDialogVisible" width="50%" @close="addressDialoClose">
      <el-form
        :model="addressForm"
        :rules="addressFormRules"
        ref="addressFormRefs"
        label-width="100px"
      >
        <el-form-item label="省县区/县" prop="address1">
          <el-cascader :options="cityData" v-model="addressForm.address1"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addressDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addressDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import cityData from './citydata'
export default {
  data () {
    return {
      // 请求需要的参数
      queryData: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 总条数
      total: 0,
      // 订单列表数据
      ordersLists: [],
      // 控制显示和隐藏对话框
      addressDialogVisible: false,
      // 地址的数据表单对象
      addressForm: {
        address1: [],
        address2: ''
      },
      // 地址的数据对象的验证
      addressFormRules: {
        address1: [
          { required: true, message: '请选择地址', trigger: 'blur' }
        ],
        address2: [
          { required: true, message: '请填写详细的地址', trigger: 'blur' }
        ]
      },
      cityData,
      // 控制查看物流的对话框
      viewAddressDialogVisible: false,
      //   logisticsInfos
      logisticsInfos: []
    }
  },
  methods: {
    // 获取订单数据列表
    async getOrderLists () {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryData
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取订单列表失败')
      }
      this.ordersLists = res.data.goods
      this.total = res.data.total
      console.log(this.ordersLists)
      console.log(this.total)
    },
    // 页码
    handleSizeChange (newSize) {
      this.queryData.pagesize = newSize
      this.getOrderLists()
    },
    // 跳转页
    handleCurrentChange (newPage) {
      this.queryData.pagenum = newPage
      this.getOrderLists()
    },
    // 点击弹出编辑地址对话框
    handelEditOrder (id) {
      this.addressDialogVisible = true
    },
    // 关闭编辑对话框触发这个事件
    addressDialoClose () {
      this.$refs.addressFormRefs.resetFields()
    },
    // 查看物流信息对话框
    async checkLogistics () {
      const { data: res } = await this.$http.get('/kuaidi/1106975712662')
      if (res.meta.status !== 200) {
        return this.$message.error('查询物流信息失败')
      }
      this.logisticsInfos = res.data
      this.viewAddressDialogVisible = true
    }
  },
  created () {
    this.getOrderLists()
  }
}
</script>

<style lang="less" scoped>
.el-table {
  margin: 15px 0;
}
.el-card {
  margin-bottom: 15px;
}
.el-cascader{
    width: 100%;
}
</style>
